package com.pc.admin.strategy.strategyImpl;

import cn.hutool.jwt.JWTUtil;
import com.pc.admin.annotation.AutoFrequency;
import com.pc.admin.annotation.Limiting;
import com.pc.admin.entity.FrequencyEntity;
import com.pc.admin.eunms.ErrorEnum;
import com.pc.admin.exception.NoSuchStrategyException;
import com.pc.admin.strategy.FrequencyStrategy;
import com.pc.admin.util.AppJwtUtil;
import io.jsonwebtoken.JwsHeader;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

/**
 * 限流ID 策略
 *
 * @author <a href="https://www.lvfeiy.top">lfy</a>
 * @date 2023/6/23 18:39
 */
@Component
@AutoFrequency
public class IdStrategy extends FrequencyStrategy {


    @Override
    public boolean analysis(Limiting.Target target) {
        return target.equals(Limiting.Target.ID);
    }

    @Override
    public FrequencyEntity service(Limiting limiting, ProceedingJoinPoint joinPoint) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        JwsHeader headerBody = AppJwtUtil.getHeaderBody(request.getHeader("xxx"));

        //TODO ID策略待定
       return null;

    }

    @Override
    public String getName() {
        return "根据用户Id 进行限流";
    }
}
